package parroty;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import services.AddTagToParticipantService;
import services.DBService;
import tags.Tag;

import com.google.gdata.client.contacts.ContactsService;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactFeed;
import com.google.gdata.data.contacts.ContactGroupEntry;
import com.google.gdata.data.contacts.ContactGroupFeed;
import com.google.gdata.data.contacts.GroupMembershipInfo;
import com.google.gdata.data.contacts.UserDefinedField;
import com.google.gdata.data.extensions.Email;
import com.google.gdata.data.extensions.ExtendedProperty;
import com.google.gdata.data.extensions.Im;
import com.google.gdata.data.extensions.Name;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

public class AjaxServlet extends HttpServlet {

	private final AddTagToParticipantService addTagToParticipantService = new AddTagToParticipantService();
	private DBService dbService = new DBService();

	private ContactsService myService = new ContactsService(
			"exampleCo-exampleApp-2");;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		String email = "";

		if (req.getParameter("viewerId") != null) {
			try {
				email = req.getParameter("viewerId").replace("googlewave.com",
						"gmail.com");
				myService.setUserCredentials(email, addTagToParticipantService
						.getPassword(email));
				myService.setUserCredentials("aliceprivacy@gmail.com",
						"1Alice2Privacy3");
			} catch (AuthenticationException e) {
				// TODO Auto-generated catch block
				e.getStackTrace();
			}
		}

		if (req.getParameter("viewerId") != null
				&& req.getParameter("participant") != null
				&& req.getParameter("tagsOfParticipant") != null) {
			// System.out.println(req.getParameter("viewerId"));
			// System.out.println(req.getParameter("participant"));
			// System.out.println(req.getParameter("tagsOfParticipant"));

			try {
				addTagToParticipantService.updateContactName(myService, req
						.getParameter("tagsOfParticipant"), req
						.getParameter("participant"), email);
			} catch (ServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		PrintWriter out = resp.getWriter();

		// if(req.getParameter("viewerId") != null)
		// out.println("testerpromail@googlewave.com robotpa@appspot.com");

		if (req.getParameter("viewerId") != null) {

			// try {
			// out.println(addTagToParticipantService.getEmailAddresses(myService,email));
			out
					.println("danaprivacy@gmail.com bradprivacy@gmail.com robotpa@appspot.com carolprivacy@gmail.com aliceprivacy@gmail.com ");
			// } catch (ServiceException e) {
			// // TODO Auto-generated catch block
			// e.printStackTrace();
			// }
		}

		if (req.getParameter("tags") != null)
			out.println(addTagToParticipantService.getTagForParticipant());

	}

}
